package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.UnqualifiedDisposalAudit;
import com.mxpio.erp.common.quality.service.UnqualifiedDisposalAuditService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "UnqualifiedDisposalAuditController", description ="不良品处理单审核详情接口" )
@RestController
@RequestMapping("/erp/quality/udaudit/")
public class UnqualifiedDisposalAuditController {

    @Autowired
    private UnqualifiedDisposalAuditService unqualifiedDisposalAuditService;

    @GetMapping("list")
    @Operation(summary = "不良品处理单审核详情列表", description = "获取不良品处理单审核详情列表（无分页）", method = "GET")
    public Result<List<UnqualifiedDisposalAudit>> list(@Parameter(name="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<UnqualifiedDisposalAudit> lines = unqualifiedDisposalAuditService.list(UnqualifiedDisposalAudit.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "不良品处理单审核详情列表", description = "获取不良品处理单审核详情列表（分页）", method = "GET")
    public Result<Page<UnqualifiedDisposalAudit>> page(@Parameter(name="查询参数") Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<UnqualifiedDisposalAudit> pagelines = unqualifiedDisposalAuditService.listPage(UnqualifiedDisposalAudit.class,page, criteria);
        return Result.OK(pagelines);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取不良品处理单审核详情", description = "根据bizNo获取不良品处理单审核详情", method = "GET")
    public Result<List<UnqualifiedDisposalAudit>> getByBizNo(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<UnqualifiedDisposalAudit> list = unqualifiedDisposalAuditService.list(UnqualifiedDisposalAudit.class,c);
        return Result.OK(list);
    }

    @PostMapping("add")
    @Operation(summary = "新增不良品处理单审核", description = "新增不良品处理单审核", method = "POST")
    public Result<UnqualifiedDisposalAudit> add(@RequestBody UnqualifiedDisposalAudit inQualityOrderLine) {
        unqualifiedDisposalAuditService.save(inQualityOrderLine);
        return Result.OK(inQualityOrderLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑不良品处理单审核", description = "编辑不良品处理单审核", method = "PUT")
    public Result<UnqualifiedDisposalAudit> edit(@RequestBody UnqualifiedDisposalAudit inQualityOrderLine) {
        unqualifiedDisposalAuditService.update(inQualityOrderLine);
        return Result.OK(inQualityOrderLine);
    }
    
    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除不良品处理单审核详情", description = "删除不良品处理单审核详情,多个id用英文逗号分隔", method = "DELETE")
    public Result<UnqualifiedDisposalAudit> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            unqualifiedDisposalAuditService.remove(UnqualifiedDisposalAudit.class, key);
        }
        return Result.OK();
    }
}

